package uk.ac.bath;

import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import uk.ac.bath.domains.EnvironmentFactory;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.ac.bath.base.EnvironmentIF;
import uk.ac.bath.base.EnvironmentFactoryIF;
import uk.ac.bath.gui.ControlPanel;
import uk.ac.bath.gui.GuiUpdater;
import uk.ac.bath.gui.View;
import uk.ac.bath.util.TweakReader;

public class LifeMain extends JFrame //implements Observer
{

    JPanel content;
    View panel;
    EnvironmentIF env;


//     public void setEnvironment(Environment env) {
//     }
    public void init() {

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu = new JMenu("Domain");
        menuBar.add(menu);
        content = new JPanel();

        Object[] options = EnvironmentFactory.getOptions().toArray();

        final String paramBase = "/params/";

        for (final Object o : options) {
            JMenuItem item = new JMenuItem(o.toString());
            menu.add(item);

            item.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {

                        public void run() {
                            if (env != null) {
                                env.dispose();
                                content.removeAll();
                            }
                            EnvironmentFactoryIF provider = ((EnvironmentFactoryIF) o);

                            String param = paramBase + provider.toString();

                            System.out.println(param);
                            try {
                                env = provider.create();
                                if (env.getTweaks().size() != 0) {
                                    InputStream reader = EnvironmentFactory.class.getResourceAsStream(param);
                                    BufferedReader bin = new BufferedReader(new InputStreamReader(reader));
                                    TweakReader.readTweakValues(bin, env.getTweaks());
                                //   env.readTweakValues(bin);
                                } else {
                                    System.out.println(" No tweaks for this environment ");
                                }
                                panel = env.createSimulationView();
                                env.setGuiUpdater(new GuiUpdater(panel));

                                content.add(panel, BorderLayout.CENTER);
                                content.add(new ControlPanel(panel, env), BorderLayout.SOUTH);
                                //  content.add(env.createEvaluationView(),BorderLayout.NORTH);
                                Thread t = new Thread(env);
                                t.start();

                                setTitle(param);
                                validate();
                            //    pack();

                            } catch (FileNotFoundException ex) {
                                System.err.println(" parameter file not found " + ex + param.toString());
                            } catch (IOException ex) {
                                System.err.println(" ERROR " + ex.getMessage());
                            } catch (Exception ex) {
                                Logger.getLogger(LifeMain.class.getName()).log(Level.SEVERE, null, ex);
                            }

                        //EnvironmentFactory.select(ans, paramBase);

                        }
                    });
                }
            });
        }

        setContentPane(content);
        validate();
        pack();
        setLayout(new BorderLayout());
        //   setExtendedState(JFrame.MAXIMIZED_BOTH);
        setMinimumSize(new Dimension(800, 600));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[]) {
        LifeMain app = new LifeMain();
        app.init();

    }
}
